<?php 
header("content-type:text/html;charset=utf8");
$pdo = new PDO("mysql:host=127.0.0.1;charset=utf8;dbname=liu",'root','root');
$data = $pdo->query("select * from hot_goods")->fetchall(PDO::FETCH_ASSOC);




 ?>
 <!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<center>
		倒计时 : <span id="hour"></span><span id="min"></span><span id="sec"></span>
		<table>
			<tr>
				<th>商品名</th>
				<th>图片</th>
				<th>原价格</th>
				<th>秒杀价</th>
				<th>库存</th>
				<th>开始时间</th>
				<th>结束时间</th>
                
                <th>操作</th>
			</tr>
			<tbody id="tbody">
				<?php foreach ($data as $key => $value): ?>
				<tr>
					<td><?php echo $value['goods_name'] ?></td>
					<td><img src="images/132240688.jpg" width="100px"></td>
					<td><?php echo $value['goods_price'] ?></td>
					<td>￥1</td>
					<td><?php echo $value['nummber'] ?></td>
					<td class="start_time"><?php echo $value['start_time'] ?></td>
					<td class="end_time"><?php echo $value['end_time'] ?></td>
					
					<td><input type="button" goods_id="<?php echo $value['goods_id'] ?>" id="button" value="抢购"></td>
					</tr>
				<?php endforeach ?>
			</tbody>
			<input type="button" id="start" value="开始">
		</table>
	</center>
</body>
</html>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
	window.setInterval(function(){
		$.getJSON("time.php","",function(res){
			 $("#hour").html(res.hour+"时");
			 $("#min").html(res.min+"分");
			 $("#sec").html(res.sec+"秒");
		})
		

	},1000);
	$("#button").click(function(){
		var goods_id = $(this).attr("goods_id");
		$.get("kill_goods.php",{goods_id:goods_id},function(res){
			alert(res.msg);
			if (res.code==1) {
				$.get("goods_order.php",{goods_id:goods_id},function(res){
					alert(res.msg);location.reload();
				},'json');
				
			}
			
		},'json')
	})
	$("#start").click(function(){
		var endtime = $("#tbody tr .end_time").text();
		var timeStr = new Date(endtime);
        timeStr = timeStr.getTime();
		var nowtime =Date.parse(new Date());
		times =timeStr-nowtime*1000;

        countDown(times);
		
	})
	function countDown(times,id=1){alert(times);
	var timer=null;		// 计时器
	timer=setInterval(
		function(){
			var day=0,hour=0,minute=0,second=0;	//时间默认值
			
			if(times<=0){
				clearInterval(timer);		// 清除计时器
				// 让按钮生效，可以点按钮
				//$('.btn').attr('disabled',false);
				$("#button").val("时间到");
			}
    
			if(times > 0){
				//$('.s-buy').on('click',function(){return false;});
				day = Math.floor(times / (60 * 60 * 24));	// 获取天
				hour = Math.floor(times / (60 * 60)) - (day * 24);	// 获取小时
				minute = Math.floor(times / 60) - (day * 24 * 60) - (hour * 60);		// 分钟
				second = Math.floor(times) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60);	// 秒
			}

			if (day <= 9) day = '0' + day;
			if (hour <= 9) hour = '0' + hour;
			if (minute <= 9) minute = '0' + minute;
			if (second <= 9) second = '0' + second;
			//
			console.log(day+"天:"+hour+"小时："+minute+"分钟："+second+"秒");

			$('#hh'+id).text(hour);
			$('#mm'+id).text(minute);
			$('#ss'+id).text(second);
			times--;
		}
		,1000);
}
</script>